/*
===========================================================================
This source file is part of DyLab (Dynamic Laboratory)
For the latest info, see http://dylab.googlecode.com

Copyright (c) 2006-2008 Lukas Krejci
(krejci.lukas@volny.cz)

This file is part of DyLab.

    DyLab is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    DyLab is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with DyLab.  If not, see <http://www.gnu.org/licenses/>.
===========================================================================
*/

#ifndef __dylab_EventDispatcher_h__
#define __dylab_EventDispatcher_h__

//===========================================================================================
#include <dylab/utils/containers/QueueContainer.h>
#include <dylab/utils/containers/HashObjContainer.h>
#include <dylab/simulation/ComEventListener.h>

//===========================================================================================
namespace dylab {

    /** 
    */
    template <
        class _EventT
    >
    class EventDispatcher
    {
    private:            
        typedef HashObjContainer<String, ComEventListener::Instance> EventListeners;
        EventListeners mEventListeners;

        typedef QueueContainer<_EventT> EventQueue;
        EventQueue mEventQueue;

    public:

        EventDispatcher()
            : mEventListeners(false)
                { }
        virtual ~EventDispatcher()
                { }

        /**
        */
        void registerListener(ComEventListener::Instance * _listener)
                { 
                    DYLAB_ASSERT(_listener != NULL);
                    mEventListeners.insert(ComEventListener::ScopedShLock(_listener)->getName(), _listener); 
                }

        /**
        */
        void unregisterListener(const String & _name)
                { mEventListeners.remove(_name); }

        /**
        */
        void unregisterAllListeners()
                { mEventListeners.removeAll(); }

        /**
        */
        void sendEvent(const _EventT & inputEvent)
                { mEventQueue.push(inputEvent); }

        /**
        */
        bool dispatchOneEvent()
                {
                    bool dispatched = false;
                    if (!mEventQueue.isEmpty())
                    {        
                        _EventT event = mEventQueue.front();
                        for (EventListeners::Iterator listener(mEventListeners); listener.isValid(); listener++)
                            ComEventListener::ScopedExLock(listener())->processEvent(event);
                        
                        mEventQueue.pop();
                        dispatched = true;
                    }
                    return dispatched;
                }

        /**
        */
        void clearEventQueue()
                { mEventQueue.removeAll(); }
    };
}
//===========================================================================================
#endif // __dylab_EventDispatcher_h__
